/*
 * @FileName: BusinessValidator.java
 * @Author
 * @Description:
 * @Date 2016年1月11日 上午11:10:36
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.validator.admin;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.validate.Validator;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.controller.admin.RewardController;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.model.Reward;
import cn.hpclub.server.model.Website;
import cn.hpclub.server.util.InterfaceUtil;

/**
 * Class description goes here
 *
 * @author
 */
public class RewardValidator extends Validator{

    final static String ORG_MESSAGE = "orgMessages";
    final static String FIRST_MESSAGE = "firstMessages";
    final static String NORMAL_MESSAGE = "normalMessages";
    final static String SINGLE_MESSAGE = "singleMessages";

    @Override
    protected void validate(Controller c){
        if(!Reward.dao.checkUnique(c.getParaToInt(Reward.ID), c.getParaToInt(Reward.ORG_ID),
                                   c.getParaToInt(Reward.BUSINESS_ID))){
            addError(ORG_MESSAGE, "当前记录已存在(分公司和业务)");
        }

        validateRegex(Reward.FIRST_REWARD, Constant.Regex.AMOUNT, FIRST_MESSAGE, "请输入 首单结酬标准(数字)");
        validateRegex(Reward.NORMAL_REWARD, Constant.Regex.AMOUNT, NORMAL_MESSAGE, "请输入正常结酬标准(数字)");
        // validateRegex(Reward.SINGLE_CAP, Constant.Regex.AMOUNT,
        // SINGLE_MESSAGE, "请输入 单项封顶(数字)");
    }

    @Override
    protected void handleError(Controller c){

        c.keepPara();
        c.setAttr(RewardController.UI_ORG, Db.find(InterfaceUtil.getOrganizationLevel1Sql()));
        c.setAttr(RewardController.UI_BUSI, Business.dao.getBusiness(false));
        c.setAttr(RewardController.UI_CHANNEL, Website.dao.getWebsite(false));
        c.setAttr(Reward.ENABLED, c.getParaToBoolean(Reward.ENABLED));
        c.render(RewardController.HTML_REWARD_INPUT);
    }
}
